package java111_2;


import java.util.Arrays;
import java.util.Scanner;

public class Day30 {
    //https://ac.nowcoder.com/acm/problem/227310
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            long a = in.nextLong();
            long b = in.nextLong();
            if((a == 1 && isS(b)) || (b == 1 && isS(a))){
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }

        }
    }
    public static boolean isS(long x){
        if(x == 1){
            return false;
        }
        for(long i = 2; i <= Math.sqrt(x); i++){
            if(x % i == 0){
                return false;
            }
        }
        return true;
    }


    //https://www.nowcoder.com/practice/562630ca90ac40ce89443c91060574c6?tpId=308&tqId=40490&ru=/exam/oj
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt(), k = in.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++){
                arr[i] = in.nextInt();
            }
            Arrays.sort(arr);
            if(arr[n-1] - arr[0] <= k){
                System.out.println(n);
                continue;
            }
            int left = 0, right = 0, res = 0;
            while(right < n){
                while(right < n && arr[right] - arr[left] < k){
                    right++;
                }
                res = Math.max(res, right-left);
                while(right < n && arr[right] - arr[left] >= k){
                    left++;
                }
            }
            System.out.println(res);
        }
    }



    //https://www.nowcoder.com/practice/672ab5e541c64e4b9d11f66011059498?tpId=230&tqId=39761&ru=/exam/oj
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        String s1 = in.next(), s2 = in.next();
        int[][] dp = new int[n+1][m+1];
        // dp[i][j] 表示 从 s1 的 (0, i) 位置 和 s2 的 (0, j) 位置 选出的最长公共子序列的长度
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(s1.charAt(i-1) == s2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        System.out.println(dp[n][m]);
    }
}
